package com.jz.community.moduleshopping.address.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.address.bean.BaseAddressInfo;

import java.util.HashMap;

/**
 * Created by Target on 2018/5/9.
 */
public class AddAddressTask extends RxTask<String, Integer, BaseAddressInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public AddAddressTask(Activity activity,
                          ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected BaseAddressInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Constant.ADD_USER_ADDRESS_URL, getParam(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8]));
        BaseAddressInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, BaseAddressInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(BaseAddressInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String address, String isDefault, String name, String phone, String addressDetails, String provinceCode, String cityCode, String areaCode, String townCode) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("address", address);
        param.put("addressDetails", addressDetails);
        param.put("isDefault", ConverterUtils.toInt(isDefault));
        param.put("addressType", 1);
        param.put("serviceType", 4);
        param.put("name", name);
        param.put("phone", phone);
        param.put("provinceCode", provinceCode);
        param.put("cityCode", cityCode);
        param.put("areaCode", areaCode);
        param.put("townCode", townCode);
        param.put("equipmentType", "android");
        param.put("userId", BaseSpUtils.getInstance().getUserId(activity));
        param.put("latitude", BaseSpUtils.getInstance().getCurrentLat(activity));
        param.put("longitude", BaseSpUtils.getInstance().getCurrentLon(activity));
        return param;
    }
}
